<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
	function goCheckPurchase() {

		// 성명 체크
		var userNameF = jQuery("input[name='userNameF']");
		var userNameS = jQuery("input[name='userNameS']");
		var hiraganaF = jQuery("input[name='hiraganaF']");
		var hiraganaS = jQuery("input[name='hiraganaS']");
		if (userNameF.val().length < 1) {
			alert('<spring:message code="error.purchase.name.none"/>');
			userNameF.focus();
			return false;
		}
		if (userNameS.val().length < 1) {
			alert('<spring:message code="error.purchase.name.none"/>');
			userNameS.focus();
			return false;
		}
		if (hiraganaF.val().length < 1) {
			alert('<spring:message code="error.purchase.name.none"/>');
			hiraganaF.focus();
			return false;
		}
		if (hiraganaS.val().length < 1) {
			alert('<spring:message code="error.purchase.name.none"/>');
			hiraganaS.focus();
			return false;
		}

		// 우편번호 체크
		var postAdd1 = jQuery("input[name='postAdd1']");
		var postAdd2 = jQuery("input[name='postAdd2']");
		if (postAdd1.val().length < 1) {
			alert('<spring:message code="error.purchase.postadd.none"/>');
			postAdd1.focus();
			return false;
		}

		if (isNumber(postAdd1.val()) == false) {
			alert('<spring:message code="error.purchase.postadd.check"/>');
			postAdd1.focus();
			return false;
		}

		if (postAdd2.val().length < 1) {
			alert('<spring:message code="error.purchase.postadd.none"/>');
			postAdd2.focus();
			return false;
		}

		if (isNumber(postAdd2.val()) == false) {
			alert('<spring:message code="error.purchase.postadd.check"/>');
			postAdd2.focus();
			return false;
		}

		var postAdd = jQuery("input[name='postAdd']");
		postAdd.val(postAdd1.val() + "-" + postAdd2.val());

		// 주소 체크
		var addr1 = jQuery("select[name='addr1']");
		var addr2 = jQuery("input[name='addr2']");
		var addr3 = jQuery("input[name='addr3']");
		if (addr1.val() == "---") {
			alert('<spring:message code="error.purchase.address.check"/>');
			addr1.focus();
			return false;
		}
		if (addr2.val().length < 1) {
			alert('<spring:message code="error.purchase.address.none"/>');
			addr2.focus();
			return false;
		}
		if (addr3.val().length < 1) {
			alert('<spring:message code="error.purchase.address.none"/>');
			addr3.focus();
			return false;
		}

		// 전화번호 체크
		var phone1 = jQuery("input[name='phone1']");
		var phone2 = jQuery("input[name='phone2']");
		var phone3 = jQuery("input[name='phone3']");
		if (phone1.val().length < 1) {
			alert('<spring:message code="error.purchase.phone.none"/>');
			phone1.focus();
			return false;
		}

		if (isNumber(phone1.val()) == false) {
			alert('<spring:message code="error.purchase.phone.check"/>');
			phone1.focus();
			return false;
		}

		if (phone2.val().length < 1) {
			alert('<spring:message code="error.purchase.phone.none"/>');
			phone2.focus();
			return false;
		}

		if (isNumber(phone2.val()) == false) {
			alert('<spring:message code="error.purchase.phone.check"/>');
			phone2.focus();
			return false;
		}

		if (phone3.val().length < 1) {
			alert('<spring:message code="error.purchase.phone.none"/>');
			phone3.focus();
			return false;
		}

		if (isNumber(phone3.val()) == false || phone3.val().length != 4) {
			alert('<spring:message code="error.purchase.phone.check"/>');
			phone3.focus();
			return false;
		}
		var phone = jQuery("input[name='phone']");
		phone.val(phone1.val() + "-" + phone2.val() + "-" + phone3.val());

		// 이메일 체크
		var userEmail = jQuery("input[name='userEmail']");
		var userEmail1_1 = jQuery("input[name='userEmail1_1']");
		var userEmail1_2 = jQuery("input[name='userEmail1_2']");
		var userEmail2_1 = jQuery("input[name='userEmail2_1']");
		var userEmail2_2 = jQuery("input[name='userEmail2_2']");

		if (userEmail1_1.val().length < 1) {
			alert('<spring:message code="error.purchase.emil.none"/>');
			userEmail1_1.focus();
			return false;
		}

		if (userEmail1_2.val().length < 1) {
			alert('<spring:message code="error.purchase.emil.none"/>');
			userEmail1_2.focus();
			return false;
		}

		if (userEmail2_1.val().length < 1) {
			alert('<spring:message code="error.purchase.emil.none"/>');
			userEmail2_1.focus();
			return false;
		}

		if (userEmail2_2.val().length < 1) {
			alert('<spring:message code="error.purchase.emil.none"/>');
			userEmail2_2.focus();
			return false;
		}

		var userEmail1 = userEmail1_1.val() + '@' + userEmail1_2.val();
		var userEmail2 = userEmail2_1.val() + '@' + userEmail2_2.val();
		if (userEmail1 != userEmail2) {
			alert('<spring:message code="error.purchase.emil.check"/>');
			userEmail1_1.focus();
			return false;
		}

		userEmail.val(userEmail1);

		var param = {
			userEmail : userEmail.val()
		};

		ajaxToJSON("/checkDuplacate.do", param, function(data) {
			jQuery("#purchaseForm").submit();
		});
	}

	function isNumber(s) {
		s += ''; // 문자열로 변환
		s = s.replace(/^\s*|\s*$/g, ''); // 좌우 공백 제거
		if (s == '' || isNaN(s)) return false;
		return true;
	}

	function chkStr(obj) {
		var chkStr = escape(obj.value);
		if (chkStr.indexOf('%') != -1) {
			alert('<spring:message code="error.purchase.number.half"/>');
			obj.value = "";
		}
	}

</script>
</head>
</html>